local utf8 = require"utf8"
DynamicAtlasManager = {}
local atlasList = {}
local atlasIndex = 1
local curAtlas = nil
function DynamicAtlasManager.pack(sf, sftype)
    if type(sf) == "number" then
        local width = sf * 2 + 1
        local radius = width
        if sf < 1 then
            width = 1
            radius = 0
        end
        sf = {
            radius = radius,
            getDimensions = function()
                return width, width
            end
        }
    end
    local width, height = sf:getDimensions()
    if width > DynamicAtlasDefaultSize or height > DynamicAtlasDefaultSize then
        return false
    end
    if not curAtlas then
        curAtlas = DynamicAtlas.new()
        table.insert(atlasList, curAtlas)
    end
    local ret = curAtlas:insert(sf, sftype)
    if not ret then
        curAtlas = DynamicAtlas.new()
        table.insert(atlasList, curAtlas)
        ret = curAtlas:insert(sf, sftype)
        if not ret then
            console.error('insertSpriteFrame error!')
            return false
        end
    end
    return ret
end

local charMap = {}
local text = love.graphics.newText(getFont(DefaultFont))
function DynamicAtlasManager.packChar(char, fontName, size)
    local key = char..fontName..size
    if not charMap[key] then
        text:clear()
        text:setFont(getFont(fontName, size))
        if char == " " then
            text:set("a a")
            local width, height = text:getDimensions()
            text:set("a")
            local w = text:getDimensions()
            width = width - 2 * w
            local sf = SpriteFrame.new(curAtlas.canvas)
            sf:setRect(0, 0, 0, 0)
            sf.width = width
            sf.height = height
            charMap[key] = sf
        else
            text:set(char)
            charMap[key] = DynamicAtlasManager.pack(text, "text")
        end
    end
    return charMap[key]
end

-- 文本内容缓存为图片
-- 过长文本需要截断
-- return 一个文本spriteFrame二维数组
function DynamicAtlasManager.packText(str, fontName, size, maxLineWidth, spacex, spacey)
    maxLineWidth = maxLineWidth or math.huge
    local curLineWidth = 0
    local str_tbl = {}
    local curLineStr = {}
    local curStr = ""
    local curStrWidth = 0
    for _, c in utf8.codes(str) do
        local charString = utf8.char(c)
        local char = DynamicAtlasManager.packChar(charString, fontName, size)

        -- 超过行宽换行截断
        if curLineWidth + spacex + char.width > maxLineWidth then
            table.insert(curLineStr, {str = curStr, width = curStrWidth})
            curLineWidth = 0
            curStrWidth = 0
            curStr = ""
            table.insert(str_tbl, curLineStr)
            curLineStr = {}
        end
        -- 超过合图size截断，但是不换行
        if curStrWidth + spacex + char.width > DynamicBatchSize then
            table.insert(curLineStr, {str = curStr, width = curStrWidth})
            curStrWidth = 0
            curStr = ""
        end
        curStrWidth = curStrWidth + char.width + spacex
        curLineWidth = curLineWidth + char.width + spacex
        curStr = curStr..charString
    end
    if curStrWidth > 0 then
        table.insert(curLineStr, {str = curStr, width = curStrWidth})
        if #str_tbl == 0 then
            table.insert(str_tbl, curLineStr)
        end
    end

    local sf_table = {}
    for i, v in ipairs(str_tbl) do
        local line = {}
        table.insert(sf_table, line)
        for _, vv in ipairs(v) do
            text:setFont(getFont(fontName, size))
            text:setf(vv.str, vv.width, "justify")
            local sf = DynamicAtlasManager.pack(text, "text")
            table.insert(line, sf)
        end
    end
    return sf_table
end

function DynamicAtlasManager.getAtlasList()
    return atlasList
end